package com.fafu.service.Impl;

import com.fafu.dao.mapper.ImageMapper;
import com.fafu.dao.mapper.ManagerMapper;
import com.fafu.dao.mapper.ManagerepairMapper;
import com.fafu.dao.mapper.StudentrepairMapper;
import com.fafu.model.Image;
import com.fafu.model.Manager;
import com.fafu.model.Managerepair;
import com.fafu.model.Studentrepair;
import com.fafu.pojo.ManagerSearchHORList;
import com.fafu.pojo.OtherSearchHORList;
import com.fafu.service.ManagerOperationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import util.Dictionary;
import util.MyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.IDEN_ERROR;
import static util.Dictionary.CODE.RECORD_NOT_EXITS;
import static util.Dictionary.CODE.SUCCESS;

@Service
public class ManagerOperationServiceImpl implements ManagerOperationService {
    @Autowired
    private StudentrepairMapper studentrepairMapper;
    @Autowired
    private ManagerepairMapper managerepairMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public Map<String,Object> refuse(Map<String,Object> map) {
        Integer id = (Integer) map.get("id");
        Studentrepair studentrepair = studentrepairMapper.selectByPrimaryKey(id);
        if(studentrepair == null)
            return new MyMap().put("code",RECORD_NOT_EXITS).put("msg","record not exits").toMap();
        studentrepair.setOperationManager((Integer) map.get("operationId"));
        studentrepair.setOperationMsg((String) map.get("msg"));
        studentrepair.setState(Dictionary.REPAIR.REPAIR_STATE_REFUSE);
        studentrepairMapper.updateByPrimaryKeySelective(studentrepair);
        return new MyMap().put("code", SUCCESS).toMap();
    }

    @Transactional
    @Override
    public Map<String, Object> repair(Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        Integer operationId = (Integer) map.get("operationId");
        Integer workerId = (Integer) map.get("workerId");
        Studentrepair studentrepair = studentrepairMapper.selectByPrimaryKey(id);
        if(studentrepair == null )
            return new MyMap()
                    .put("code",Dictionary.CODE.RECORD_NOT_EXITS)
                    .put("msg","记录不存在哟").toMap();
        studentrepair.setOperationManager(operationId);
        studentrepair.setState(Dictionary.REPAIR.REPAIR_STATE_HANDING);
        studentrepairMapper.updateByPrimaryKeySelective(studentrepair);

        Managerepair managerepair = new Managerepair();
        managerepair.setStudengRepairId(id);
        managerepair.setState(Dictionary.REPAIR.MANAGE_REPAIR_NO_READ);
        managerepair.setManagerId(operationId);
        managerepair.setWorkerId(workerId);
        managerepair.setAddTime(System.currentTimeMillis());
        managerepairMapper.insertSelective(managerepair);
        return new MyMap().put("code",SUCCESS).toMap();
    }

    @Override
    public Map<String, Object> managerSearchRepair(Map<String, Object> map) {
        Integer size = (Integer) map.get("size");
        Integer operationId = (Integer) map.get("operationId");
        if(size == 0) {
            Manager manager = managerMapper.selectByPrimaryKey(operationId);
            if(manager == null)
                return new MyMap().put("code",IDEN_ERROR).toMap();
            map.remove("operationId");
            map.put("area",manager.getArea());
        }
        PageHelper.startPage((Integer) map.get("pageNum"),(Integer) map.get("pageSize"));
        List<ManagerSearchHORList> reList = managerepairMapper.managerSearchList(map);
        PageInfo pageInfo = new PageInfo(reList);
        return new MyMap().put("code",SUCCESS).put("data",pageInfo.getList()).put("count",pageInfo.getTotal()).toMap();
    }

    @Override
    public Map<String,Object> changeUsed(Map<String, Object> paramMap) {
        Integer opid = (Integer) paramMap.get("opid");
        Integer chid = (Integer) paramMap.get("chid");
        Manager opManager = managerMapper.selectByPrimaryKey(opid);
        Manager chManager = managerMapper.selectByPrimaryKey(chid);
        if(opManager == null || chManager == null)
            return new MyMap()
                    .put("code",IDEN_ERROR)
                    .put("msg","账号不存在").toMap();
        if(chManager.getPower() >= opManager.getPower() || opManager.getIsUsed() == 0)
            return new MyMap()
                    .put("code", Dictionary.CODE.NO_OPERATION_POWER)
                    .put("msg","没有操作权限").toMap();

        chManager.setIsUsed( (chManager.getIsUsed()+1)%2);
        Integer re = managerMapper.updateByPrimaryKeySelective(chManager);
        if(re == 1)
            return new MyMap().put("code",SUCCESS).put("msg","success").toMap();
        return new MyMap().put("code", Dictionary.CODE.SYSTEM_ERROR).put("msg","no know error").toMap();
    }

    @Override
    public Map<String, Object> managerSearchOtherRepair(Map<String, Object> map) {
        Integer size = (Integer) map.get("size");
        Integer operationId = (Integer) map.get("operationId");
        if(size == 0) {
            Manager manager = managerMapper.selectByPrimaryKey(operationId);
            if(manager == null)
                return new MyMap().put("code",IDEN_ERROR).toMap();
            map.remove("operationId");
            map.put("area",manager.getArea());
        }
        PageHelper.startPage((Integer)map.get("pageNum"),(Integer)map.get("pageSize"));
        List<OtherSearchHORList> reList = managerepairMapper.managerSearchOtherRepair(map);
        PageInfo pageInfo = new PageInfo(reList);
        return new MyMap().put("code",SUCCESS).put("data",pageInfo.getList()).put("count",pageInfo.getTotal()).toMap();
    }

}
